﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CeA.Reports.Atendimento.DataAccess.DTO;
using System.Collections.Generic;
using CeA.Reports.Atendimento.Web.ServerControls;
using CeA.Reports.Atendimento.Web.Helpers;
using CeA.Reports.Atendimento.DataAccess.Criteria;
using Qtech.Reports.Atendimento.Services;
using CeA.Reports.Atendimento.Web.WebControls;

namespace CeA.Reports.Atendimento.Web.admin {
    public partial class rfs : System.Web.UI.Page {
        
        protected void Page_Init(object sender, EventArgs e)                    {
            cmdPesquisar.Click += new EventHandler(cmdPesquisar_Click);
            MessageBox.Command += new MessageBox.CommandEventHandler(MessageBox_Command);
        }


        protected void Page_Load(object sender, EventArgs e)                    {
            menuInterno.addMenuItem("Incluir Nova RFS", "/admin/rfscadastro.aspx");
        }


        protected void cmdPesquisar_Click(object sender, EventArgs e)           {
            CarregarLista();
        }


        protected void cmdEditar_Click(object sender, EventArgs e)              {
            List<GroupRadioButton> rbtList = DatalistHelper.FindControl<GroupRadioButton>(dtlRfs, "rbtItem");

            foreach (GroupRadioButton rbt in rbtList) {
                if (rbt.Checked) {
                    CodigoRfs = Convert.ToInt32(rbt.Text);
                    Server.Transfer("rfscadastro.aspx", true);
                }
            }

            MessageBox.pop("É necessário selecionar um registro para editar!");
        }


        protected void cmdExcluir_Click(object sender, EventArgs e)             {
            List<GroupRadioButton> rbtList = DatalistHelper.FindControl<GroupRadioButton>(dtlRfs, "rbtItem");

            foreach (GroupRadioButton rbt in rbtList) {
                if (rbt.Checked) {
                    CodigoRfs = Convert.ToInt32(rbt.Text);

                    MessageBox.pop("Deseja excluir o registro selecionado?", MessageBoxButtoms.YesNo);
                    MessageBox.CommandName = "DELETE";

                    return;
                }
            }

            MessageBox.pop("É necessário selecionar um registro para excluir!");
        }


        protected void MessageBox_Command(object sender, MessageBoxEventArgs e) {
            if (e.CommandName == "DELETE" && e.Command == MessageBoxCommand.Yes) {
                try
                {
                    CadastroFacade.RemoveRfs(CodigoRfs);
                }
                catch (InvalidOperationException ex)
                {
                    MessageBox.pop(ex.Message);
                }
                CarregarLista();
            }
        }


        private void CarregarLista()                                            {
            RfsCriteria filter = new RfsCriteria();
            filter.Nome = txtNome.Text;

            List<RfsDTO> ds = CadastroFacade.FindRfs(filter);

            dtlRfs.DataSource = ds;
            dtlRfs.DataBind();
        }


        public int CodigoRfs                                                    {
            get {
                if (ViewState["m_codigoRfs"] != null)
                    return Convert.ToInt32(ViewState["m_codigoRfs"]);
                return 0;
            }
            set {
                ViewState.Add("m_codigoRfs", value);
            }
        }
    }
}
